package apiFun

import (
	"cake-server/module"
	"cake-server/utils"
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
)

type searchInfo struct {
	module.Menu
	Limit
}

func GetMenu(c *gin.Context) {
	var searchInfo searchInfo
	searchInfo.PageIndex = 1
	searchInfo.PageSize = 10 //默认值
	err := c.ShouldBind(&searchInfo)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "参数错误",
		})
		return
	}

	var toatalCount int64
	module.DB.Model(&module.Menu{}).Where("name like ?", utils.VagueFormat(searchInfo.Name)).Count(&toatalCount)
	var menus []module.Menu
	result := module.DB.Limit(searchInfo.PageSize).Offset((searchInfo.PageIndex-1)*searchInfo.PageSize).Where("name like ?", utils.VagueFormat(searchInfo.Name)).Find(&menus)
	if result.Error == nil {
		resolveMenus := utils.ResolveMenus(menus)
		c.JSON(http.StatusOK, gin.H{
			"code":       200,
			"message":    "获取菜单成功",
			"data":       resolveMenus,
			"totalCount": toatalCount,
		})
	} else {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "获取菜单失败",
			"error":   result.Error,
		})
	}
}

func AddMenu(c *gin.Context) {
	var menu module.Menu
	err := c.ShouldBind(&menu)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "参数错误",
			"error":   err.Error(),
		})
		return
	}
	result := module.DB.Where("name = ?", menu.Name).First(&menu)
	if result.RowsAffected > 0 {
		c.JSON(http.StatusOK, gin.H{
			"code":    400,
			"message": "菜单已存在",
		})
		return
	}
	result = module.DB.Create(&menu)
	if result.RowsAffected > 0 {
		c.JSON(http.StatusOK, gin.H{
			"code":    200,
			"message": "添加菜单成功",
		})
		return
	} else {
		c.JSON(http.StatusBadRequest, result.Error)
	}
}

func UpdateMenu(c *gin.Context) {
	var menu module.Menu
	err := c.ShouldBind(&menu)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "参数错误",
		})
	}
	fmt.Println("__________________________________")
	fmt.Println(menu)
	fmt.Println("__________________________________")
	result := module.DB.Where("id = ?", menu.Id).First(&module.Menu{})
	if result.RowsAffected == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code":    400,
			"message": "菜单不存在",
		})
		return
	}
	result = module.DB.Model(&menu).Updates(menu)
	if result.RowsAffected == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code":    400,
			"message": "更新菜单失败",
			"error":   result.Error,
		})
		return
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code":    200,
			"message": "更新菜单成功",
		})
	}
}

func DeleteMenu(c *gin.Context) {
	var ids []int
	err := c.ShouldBind(&ids)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "参数错误",
		})
		return
	}
	result := module.DB.Where("id IN (?)", ids).Delete(&module.Menu{})
	if result.RowsAffected > 0 {
		c.JSON(http.StatusOK, gin.H{
			"code":    200,
			"message": "删除菜单成功",
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code":    400,
			"message": "删除失败",
			"error":   result.Error,
		})
	}
}
